#pragma once
#include <deque>
#include <map>
#include <mutex>

using namespace std;

struct Position
{
	int XPos;
	int YPos;
	char Id;
};
mutex MxPlayerQueue;
mutex MxGameQueue;

class Player
{
private:
	char Up, Left, Down, Right;
	struct Position Pos;
	int Area;
	deque<char> *PlayerQueue;
	deque<Position> *GameQueue;

public:

	Player(void)
	{
		Up = 0;
		Left = 0;
		Down = 0;
		Right = 0;
		Pos.XPos = 0;
		Pos.YPos = 0;
	}

	Player(char Up, char Left, char Down, char Right, char PosId, 
		int XPos, int YPos, int Area, deque<char> &PlayerQueue, deque<Position> &GameQueue)
	{
		this->Up = Up;
		this->Left = Left;
		this->Down = Down;
		this->Right = Right;
		this->Pos.Id = PosId;
		this->Pos.XPos = XPos;
		this->Pos.YPos = YPos;
		this->PlayerQueue = &PlayerQueue;
		this->GameQueue = &GameQueue;
		this->Area = Area;
		MxGameQueue.lock();
		GameQueue.push_back(Pos);
		MxGameQueue.unlock();
	}

	void MovePlayer()
	{
		MxPlayerQueue.lock();
		if (!PlayerQueue->empty())
		{
			char c = PlayerQueue->front();
			PlayerQueue->pop_front();

			if( c == Up && Pos.YPos > 0)
				Pos.YPos = Pos.YPos - 1;
			else if( c == Left && Pos.XPos > 0)
				Pos.XPos = Pos.XPos - 1;
			else if( c == Down && Pos.YPos < Area - 1)
				Pos.YPos = Pos.YPos + 1;
			else if( c == Right && Pos.XPos < Area - 1)
				Pos.XPos = Pos.XPos + 1;
			MxGameQueue.lock();
			GameQueue->push_back(Pos);
			MxGameQueue.unlock();
		}
		MxPlayerQueue.unlock();
	}

	void operator()()
	{
		while(true)
			MovePlayer();
	}

	~Player(void)
	{
	}
};

